//
// Created by vista on 2018/1/6.
//


#include <vector>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <set>
using namespace std;

class Solution {
public:
    int divide(int dividend, int divisor) {
        if(divisor == 0)
            return INT32_MAX;
        else {
            long result = 0;
            bool negative = true;
            if ((dividend < 0 && divisor < 0) || (dividend > 0 && divisor > 0)) {
                negative = false;
            }

            long valend = abs((long)dividend);
            long sor = abs((long)divisor);

            if (sor == 1)
                result = valend;
            else if (sor == 2)
                result = valend >> 1;
            else {
                do {
                    valend -= sor;
                    ++result;
                } while (valend >= 0);
                result--;
            }

            if(result > INT32_MAX){
                if(!negative || result - 1 != INT32_MAX)
                {
                    return INT32_MAX;
                }
            }

            if( !negative )
            {
                return static_cast<int>(result);
            }else
                return static_cast<int>(-result);
        }
    }
};

int main(){

    Solution solution;

    __uint64_t timebegin =  clock_gettime_nsec_np(CLOCK_REALTIME);


    cout << solution.divide(1,-2) << endl;

    __uint64_t timeend =  clock_gettime_nsec_np(CLOCK_REALTIME);

    cout << (timeend - timebegin) / 1000000.0 << endl;

    return 0;

}